let plugin = requirePlugin("WechatSI");
let manager = plugin.getRecordRecognitionManager();
const modalChildBehavior = require('../../behaviors/modalChildBehavior');
Component({
  data: {
    activeIndex: 0,
    recording: false,
    answerShow: false,
    isRight: false
  },
  properties: {
    show: {
      type: Boolean,
      value: false
    },
    text: {
      type: String,
      value: '...'
    }
  },
  behaviors: [ modalChildBehavior ],
  attached() {
    this.result = '';
    let self = this;
    manager.onRecognize = (res) => {
      console.log( 'recognize' )
      console.log( res );
    }
    manager.onStop = (res) => {
      console.log( 'on record stop' )
      let wordStr = self.data.text.toLowerCase();
      let recordStr = res.result.toLowerCase();
      recordStr = recordStr.split('.')[0];
      self.onRecordStop( recordStr );
    }
    manager.onError = (res) => {
      console.log( res );
      self.onRecordStop( '' );
    }
  },
  methods: {
    // 未使用
    _showObserver( val ) {
      let self = this;
      if ( val ) {
        this.startStreamRecord();
        this.timeoutTimer = setTimeout(() => {
          console.log( '录音超时...' )
          self.closeRecordModal();
        }, 5000);
      } else {
        this.closeRecordModal();  
      }
    },
    clearTimer( timer ) {
      clearTimeout( timer );
      timer = null;
    },
    closeRecordModal() {
      this.setDataSmart({
        show: false
      })
      this.stopAnimation();
      this.clearTimer( this.timeoutTimer );
    },
    onRecordStop( text ) {
      let self = this;
      this.setData({
        isRight: text == this.data.text
      })
      this.closeRecordModal();
      let timer = setTimeout(() => {
        self.answerViewShow();
        clearTimeout( timer );
        timer = null;
      }, 200);
    },
    answerViewShow() {
      this.setData({
        answerShow: true
      })
      let self = this;
      let timer = setTimeout(() => {
        self.setData({
          answerShow: false
        })
        clearTimeout( timer );
        timer = null;
      }, 1000);
    },
    startStreamRecord() {
      console.log( '录音中' );
      wx.stopBackgroundAudio();
      manager.start({duration:5000, lang: "en_US"});
      this.startAnimation();
    },
    endStreamRecord() {
      console.log('touch end');
      this.closeRecordModal();
      this.stopAnimation();
      manager.stop();
    },
    startAnimation( val ) {
      console.log( '开始录音' );
      let self = this;
      let index = 0;
      this.interval = setInterval(() => {
        index ++;
        self.setData({
          activeIndex: index
        })
        if ( index >= 3 ) {
          index = -1;
        }
      }, 300);
      
    },
    stopAnimation() {
      clearInterval( this.interval );
      this.interval = null;
      this.setData({
        activeIndex: 0
      })
    },
    onTapShade() {  
      // manager.stop();
      this.stopAnimation();
      this.setDataSmart({
        show: false
      })
    },
    onCatchTap() {}
    
  }
})